﻿using System;
using System.Collections.Generic;
using System.Data.Entity; // needed for lambda includes
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;

namespace TheCup.Controllers
{
    public class EventController : Controller
    {
        ApiControllers.EventController ec = new ApiControllers.EventController();
        ApiControllers.CommentController apicc = new ApiControllers.CommentController();
        ApiControllers.PlayerController pc = new ApiControllers.PlayerController();
        ApiControllers.CourseController c = new ApiControllers.CourseController();

        public ActionResult Index()
        {
            using (TheCupContext ctx = new TheCupContext())
            {
                var events = ctx.Events.Include(c => c.Course).OrderByDescending(e => e.StartDate).ToList();
                return View(events);
            }
        }

        [Authorize]
        public ActionResult Details(int id)
        {
            Event e = ec.Get(id);
            Player p = pc.GetLoggedInPlayer();
            ViewBag.JoinedAlready = ec.PlayerJoinedEvent(e.Id, p.Id);

            // we've failed to maintain uuid for comment system
            Debug.Assert(e.uuid != null);

            CommentController cc = new CommentController();
            e.Comments = cc.Get(e.uuid, e.Id, CommentEntity.Event);
            
            return View(e);
        }

        [Authorize]
        public ActionResult Create()
        {
            Player p = pc.GetLoggedInPlayer();
            Event e = new Event();
            e.StartDate = DateTime.Today;
            e.JoinByDate = DateTime.Today;
            
            e.uuid = Guid.NewGuid();
            e.PlayerId = p.Id;
            e.LegacyOwnerUuid = p.uuid;

            ViewBag.Courses = c.Get();

            return View(e);
        }

        [Authorize]
        [HttpPost]
        public ActionResult Create(Event e)
        {
            if (ModelState.IsValid)
            {
                ec.Post(e);
            }
            
            return Redirect("~/");
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            Event e = ec.Get(id);
            ViewBag.Courses = c.Get();
            return View(e);
        }
        
        [Authorize]
        [HttpPost]
        public ActionResult Edit(Event e)
        {
            if (ModelState.IsValid)
            {
                if (e.PlayerId == pc.GetLoggedInPlayer().Id)
                {
                    ec.Put(e.Id, e);
                }
                else
                {
                    ViewBag.Courses = c.Get();
                    ModelState.AddModelError("owner", "Du ejer ikke denne event!");
                    return View(e);
                }
            }

            return Redirect("~/");
        }

        public ActionResult Join(int id)
        {
            Player pl = pc.GetLoggedInPlayer();
            Event ev = ec.Get(id);
            ViewBag.JoinedAlready = ec.PlayerJoinedEvent(id, pl.Id);
            
            // This doesn't work unless you return the model
            if (ev.DateDeleted != null) 
            {
                ModelState.AddModelError("expired", "event er udløbet");
            }
            // same
            else if (ec.PlayerJoinedEvent(ev.Id, pl.Id))
            {
                ModelState.AddModelError("joined", "spiller allerede tilmeldt");
            }
            else
            {
                ec.JoinEvent(ev, pl);
            }

            return RedirectToAction("Details", new { id = id });
        }

        public ActionResult Leave(int id)
        {
            ec.Leave(id);

            return RedirectToAction("Details", new { id = id });
        }

        [Authorize]
        public ActionResult Delete(int id)
        {
            ec.Delete(id);
            return Redirect("~/");
        }
    }
}
